package com.jml.jdk8.日期;


import java.time.Clock;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.MonthDay;
import java.time.Period;
import java.time.YearMonth;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoUnit;
import java.util.Set;
import java.util.TreeSet;
import org.junit.Test;

/**
 * @author:jml
 * @date: 2020/3/4 0004 11:39
 * @description：JDK8d的时间基本都是来自joda-time，
  * @jdk8所有日期和时间都是不可变的，线程安全的
  * @现有的date都是可变的非线程安全的；
 */
public class Jdk8TimeTest {

    /**
     * LocalDate就是不关注时分秒，只关注年月日
     */
    @Test
    public void localDate(){
        LocalDate localDate = LocalDate.now();
        System.out.println("年："+localDate.getYear());
        //返回数字，从1开始，date是从0开始
        System.out.println("月："+localDate.getMonthValue());
        //返回字母枚举
        System.out.println("月："+localDate.getMonth());
        System.out.println("日："+localDate.getDayOfMonth());
        System.out.println("--------------------------------------------------------------------");
        //年月日的构造LocalDate
        LocalDate localDate2 = LocalDate.of(2017, 2, 1);
        System.out.println(localDate2);
    }
    /**
     *A是否在B之前 A.isBefore(B);
     *isEqual方法底层直接已经改了，比较年月日
     */
    @Test
    public void localDateBeforeAfter(){
        LocalDate localDate = LocalDate.now();
        LocalDate localDate2 = LocalDate.of(2017, 2, 1);
        LocalDate localDate3 = LocalDate.of(2017, 2, 1);
        boolean before = localDate.isBefore(localDate);
        boolean equal = localDate2.isEqual(localDate3);
        System.out.println(equal);
        System.out.println(before);
    }
    /**
     * MonthDay就是不关注年，只关注月日
     */
    @Test
    public void  MonthDay(){  //年月日的构造LocalDate
        LocalDate localDate2 = LocalDate.of(2017, 2, 1);
        //月天构造MonthDay,不关注年
        MonthDay monthDay = MonthDay.of(2, 1);
        MonthDay monthDay1 = MonthDay.from(localDate2);
        //比较月和日是不是同一天
        System.out.println(monthDay.equals(monthDay1));
    }

    /**
     * LocalTime就是不关注年月日，只关注时分秒
     */
    @Test
    public void  LocalTime(){
       LocalTime localTime = LocalTime.now();
        System.out.println(localTime);
        //增加3小时5分钟
        LocalTime localTime1 = localTime.plusHours(3).plusMinutes(5);
        System.out.println(localTime1);
    }


    /**
     * plus方法
     */
    @Test
    public void  LocalDatePlus(){
        LocalDate localDate = LocalDate.now();
        //增加两周
        localDate.plus(2, ChronoUnit.WEEKS);
    }

    /**
     * clock,时区，ZoneId，时区
     * ZonedDateTime带有时区的时间
     */
    @Test
    public void  clock(){
        Clock clock = Clock.systemDefaultZone();
        ZoneId zone = clock.getZone();
        System.out.println(zone);
        //得到毫秒
        System.out.println(clock.millis());
        System.out.println("--------------------------------------------------------------------");
        //得到所有的时区,set无序
        Set<String> zoneIds = ZoneId.getAvailableZoneIds();
        //放入TreeSet排序
        Set<String> treeSet = new TreeSet<String>() {
            {
                addAll(zoneIds);
            }
        };
        treeSet.stream().forEach(item -> System.out.println(item));
        System.out.println("--------------------------------------------------------------------");
        //时区
        ZoneId zoneId = ZoneId.of("Asia/Shanghai");
        //时间
        LocalDateTime localDateTime = LocalDateTime.now();
        //时间后面有时区
        ZonedDateTime zonedDateTime = ZonedDateTime.of(localDateTime, zoneId);
        System.out.println(localDateTime);
        System.out.println(zonedDateTime);
    }

    /**
     *yearMonth关注年月
     */
    @Test
    public void  yearMonth(){
        YearMonth yearMonth = YearMonth.now();
        //当前月有多少天
        System.out.println(yearMonth.lengthOfMonth());
        //当前年有多少天
        System.out.println(yearMonth.lengthOfYear());
        //是否是闰年
        System.out.println(yearMonth.isLeapYear());
        System.out.println("---------------------------------------------------------------");
        YearMonth yearMonth1 = YearMonth.of(2017,2);
        //当前月有多少天
        System.out.println(yearMonth1.lengthOfMonth());
        //当前年有多少天
        System.out.println(yearMonth1.lengthOfYear());
        //是否是闰年
        System.out.println(yearMonth1.isLeapYear());

    }
    /**
     * 得到。两个时间的差，B减去A，可以为负数
     *Period.between(A, B);
     * Period这个是周期性的
     * 时间相差period.getYears()年，period.getMonths()月，period.getDays()天
     * 如果是30天那就是0-1-0  1*30+0
     * 如果是32天那就是0-1-2  1*30+2
     */
    @Test
    public void  period(){
        LocalDate localDate = LocalDate.of(2020, 5, 16);
        LocalDate localDate1 = LocalDate.of(2020, 4, 15);
        Period period = Period.between(localDate1, localDate);
        //得到相差的年
        System.out.println(period.getYears());
        //得到相差的月
        System.out.println(period.getMonths());
        //日
        System.out.println(period.getDays());
        System.out.println("相差"+period.getYears()+"年"+period.getMonths()+"月"+period.getDays()+"日");
    }

    /**
     * instant表特定的一个时刻
     */
    @Test
    public void  instant(){
        Instant instant = Instant.now();
        //2020-03-04T06:31:11.298Z是一个不带时区的标准的UTC时间的输出
        System.out.println(instant);
    }



}
